import java.io.*;
import java.util.Arrays;

/*
 * Assignment: 1
 * Author:
 * Due: June 2, 2014
 *
 *
 */

import java.io.*;
import java.util.Arrays;


public class Assign1 {
	public static int ctr = 0;

    public static void main(String[] args) throws IOException{
        double[] sides = new double[3]; 
        boolean continueProcessing = true;
        boolean isValidTriangle = false;
        
        System.out.println("This program will accept 3 numbers that represent the sides of a triangle.");
        System.out.println("Based on the numbers entered, the program will identify whether the triangle is:");
        System.out.println("1. Scalene,");
        System.out.println("2. Isosceles,");
        System.out.println("3. Equilateral, and/or a");
        System.out.println("4. Right Angle");
        System.out.println();
        System.out.println("NOTES: ");
        System.out.println("1) Alphanumeric characters, zero, and negative numbers are considered invalid.");
        System.out.println("2) A triangle is invalid if the sum of any 2 sides is not greater than the 3rd side.");
      
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);

        System.out.println();
        while (continueProcessing)
        {
        	System.out.println("Enter the length for each side of a triangle: ");
        	
        	while (ctr < 3){
	        	
	        	System.out.print("side " + (ctr + 1) + ": ");
	        	
	        	try{
	        		sides[ctr] = validateInput(in.readLine());
	           		ctr++;
	        	}  
	        	catch(NumberFormatException nfe){
	        		System.out.println("The number you entered is invalid! Please try again.");
	        	} 
	        }
	        
	       isValidTriangle = validateTriangle(sides);
	       if (isValidTriangle) {
	    	   identifyTriangle(sides);
	       }
	        
	       ctr = 0;
	       System.out.println();
	       System.out.println("Press enter to continue or type 'exit' to end.");
	       String end = in.readLine();
	       if (end.equals("exit")){
		       System.out.println("Good bye.");
		       System.out.println(" ");
		       break;
		       
	       }       
        }
    }
    
    private static boolean validateTriangle(double[] sides) {
    	if (!(((sides[0] + sides[1]) > sides[2]) && ((sides[1] + sides[2]) > sides[0]) && ((sides[0] + sides[2]) > sides[1]))) {
    		   System.out.println("Not A Triangle");
    		   return false;
    		}
		return true;
	}

    //validateInput will check for non-numeric/non-zero value and throw an Exception if either situation is encountered.
    private static double validateInput(String value) {
    	
    	//Failure to enter a number will result in an Exception.
    	//Exception will be handled by the calling method.
    	double d = 0;
    	d = Double.parseDouble(value);
    	
    	//Force user to enter a non-zero, positive value
    	if (d <= 0){
    		throw new NumberFormatException();
    	}
		return d;
	}
	
    //identifyTriangle will accept an array of type double, evaluate the values in the array and determine the Triangle type
    private static void identifyTriangle(double[] sides) {
    	
    	//Sort array to make identifying a Right Triangle easier
		Arrays.sort(sides);   	
   	
		if (sides[0] == sides[1] && sides[1] == sides[2]){
			System.out.print("Equilateral Triangle");
		}
		else if (sides[0] != sides[1] && sides[1] != sides[2] && sides[0] != sides[2]) {
			System.out.print("Scalene Triangle");
		}
		else {
			System.out.print("Isosceles Triangle");		
		}
		
		if (Math.pow(sides[0],2) + Math.pow(sides[1],2) == Math.pow(sides[2],2)) {
			System.out.println(" and Right Triangle");
		}
		else {
			System.out.println("");
		}
	}

}
